<?php
// 订单模型
class OrderModel extends Model {
	/**录入订单
	 * 
	  `shelf_id` bigint(20) unsigned NOT NULL,
	  `user_id` bigint(20) unsigned NOT NULL COMMENT '下订单者ID',
	  `quantity` int(10) unsigned NOT NULL COMMENT '购买数量',
	  `pay_id` varchar(32) DEFAULT NULL COMMENT '付款纪录ID，记录支付的总金额',
	  `pay_time` int(10) unsigned DEFAULT NULL COMMENT '支付时间',
	  `pay_state` enum('unpay','pay') NOT NULL DEFAULT 'unpay' COMMENT '支付状态',
	  `paid_money` double(10,2) NOT NULL DEFAULT '0.00' COMMENT '支付金额，是否需要同pay_id中的字段进行验证',
	  `used_credit` double(10,2) NOT NULL DEFAULT '0.00' COMMENT '积分支付额度，积分支付整个系统积分与现金的换算比例是相同的',
	  `gain_credit` double(10,2) NOT NULL DEFAULT '0.00' COMMENT '实际的反利',
	  `zipcode` char(6) DEFAULT NULL,
	  `address` varchar(128) DEFAULT NULL,
	  `province_id` int(11) unsigned DEFAULT '0',
	  `city_id` int(11) unsigned DEFAULT '0',
	  `area_id` int(11) unsigned DEFAULT '0',
	  `order_state` enum('Y','N') NOT NULL DEFAULT 'Y' COMMENT '订单状态，是否撤销（未支付的可撤销）。由用户执行的操作',
	  `enable` enum('Y','N') NOT NULL DEFAULT 'Y' COMMENT '是否可用。有管理员执行，用于标记订单失效',
	  `remark` varchar(128) DEFAULT NULL COMMENT '用于记录（当enable=N时），管理员撤销订单原因',
	  `ctime` int(10) unsigned NOT NULL COMMENT '订单生成时间',
	  `mtime` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '订单修改时间',

	 **/
	public function addOrder($shelf_id,$user_id,$sender_name,$sender_mobile
								,$quantity,$paid_money,$used_credit,$gain_credit){
		$order['shelf_id']=$shelf_id;// bigint(20) unsigned NOT NULL,
		$order['user_id']=$user_id;// bigint(20) unsigned NOT NULL COMMENT '下订单者ID',
		$order['quantity']=$quantity;// int(10) unsigned NOT NULL COMMENT '购买数量',
		$order['pay_time']=null;// int(10) unsigned DEFAULT NULL COMMENT '支付时间',
		$order['pay_state']='unpay';// enum('unpay','pay') NOT NULL DEFAULT 'unpay' COMMENT '支付状态',
		$order['paid_money']=$paid_money;// double(10,2) NOT NULL DEFAULT '0.00' COMMENT '支付金额，是否需要同pay_id中的字段进行验证',
		$order['used_credit']=$used_credit;// double(10,2) NOT NULL DEFAULT '0.00' COMMENT '积分支付额度，积分支付整个系统积分与现金的换算比例是相同的',
		$order['gain_credit']=$gain_credit;// double(10,2) NOT NULL DEFAULT '0.00' COMMENT '实际的反利',
		$order['order_state']='N';// enum('Y','N') NOT NULL DEFAULT 'Y' COMMENT '订单状态，是否撤销（未支付的可撤销）。由用户执行的操作',
		$order['enable']='Y';// enum('Y','N') NOT NULL DEFAULT 'Y' COMMENT '是否可用。有管理员执行，用于标记订单失效',
		$order['remark']='';// varchar(128) DEFAULT NULL COMMENT '用于记录（当enable=N时），管理员撤销订单原因',
		$order['ctime']=time();// int(10) unsigned NOT NULL COMMENT '订单生成时间',
		$order['sender_name']=$sender_name;// int(10) unsigned NOT NULL COMMENT '订单生成时间',
		$order['sender_mobile']=$sender_mobile;// int(10) unsigned NOT NULL COMMENT '订单生成时间',
		
		$id=$this->_createId($shelf_id);
		//echo '_createId='.$id;
		$order['id']=$id;
		//$order['mtime']='';// int(10) unsigned NOT NULL DEFAULT '0' COMMENT '订单修改时间',
		$this->add($order);
		//echo 'no='.$no;
		$tmp = $this->where('id='.$id)->find();
		if(!empty($tmp))return $id;
		return 0;
	}
	
	/** 生成订单ID **/
	private function _createId($shelf_id){
		return $shelf_id.time().rand(1000, 9999);
	}
	
	/**用户删除订单
	 * 
	 * @param unknown_type $order_id
	 * @param unknown_type $user_id
	 */
	public function userDeleteOrder($order_id,$user_id){
		if(empty($order_id)||empty($user_id))return 0;
		$order['id']=$order_id;		
		$order['user_id']=$user_id;
		$order['order_state']=array('neq','Y');
		$no = $this->where($order)->save(array(	 'mtime'=>time()
												,'order_state'=>'Y'
											 )
										);	
		return $no;
	}
	
	
	/**
	 * 更新订单为已支付。
	 * @param String $order_id
	 */
	public function upd2Payed($order_id){
		$data['pay_time']=time();//` int(10) unsigned DEFAULT NULL COMMENT '支付时间',
		$data['pay_state']='pay';//` enum('unpay','pay') NOT NULL DEFAULT 'unpay' COMMENT '支付状态',
		$map_1['id']=$order_id;
		$map_1['pay_state']=array('neq','pay');$map_1['order_state']='N';$map_1['enable']='Y';
		$no = model('Order')->where($map_1)->save($data);
		return $no;
	}
	
	/**用户删除订单
	 * 
	 * @param unknown_type $order_id
	 * @param unknown_type $user_id
	 */
	public function userUnshowOrder($order_id,$user_id){
		if(empty($order_id)||empty($user_id))return 0;
		$order['id']=$order_id;		
		$order['user_id']=$user_id;
		$no = $this->where($order)->save(array(	 'mtime'=>time()
												,'is_show'=>'N'
											 )
										);	
		return $no;
	}
	
	
	/**用户删除订单
	 * 
	 * @param unknown_type $order_id
	 * @param unknown_type $user_id
	 */
	public function adminDeleteOrder($order_id,$admin_id,$remark){
		if(empty($order_id)||empty($admin_id))return 0;
		$order['id']=$order_id;		
		//$order['user_id']=$user_id;
		$no = $this->where($order)->save(array(	 'mtime'=>time()
												,'enable'=>'N'
												,'remark'=>$remark
											 )
										);	
		return $no;
	}
	
	
	/** 用户查看订单,查询未撤销的订单 **/
	public function selectById($id){
		$where['id']			=$id;
		//if(!empty($uid))$where['user_id']		=$uid;
		$where['order_state']	='N';//` enum('Y','N') NOT NULL DEFAULT 'Y' COMMENT '订单状态，是否撤销（未支付的可撤销）。由用户执行的操作',
	  	//$where['enable']		='Y';//` enum('Y','N') NOT NULL DEFAULT 'Y' COMMENT '是否可用。有管理员执行，用于标记订单失效',
		//dump($where);
	  	$order = $this->where($where)->find();
	  	//dump($order);
		return $order;
	}
	
	/**
	 * 删除订单。删除订单的同时，将删除对应的支付记录、礼券信息。
	 * @param mix $oids
	 */
	public function deleteOrder($oids,$remark='未填写！'){
		$this->startTrans();
		//删除订单
		$map['id']	= array('in', $oids);
		$map['enable']	= 'Y';
		
		$ary['enable']	=	'N';
		$ary['remark']	=	$remark;
		$ary['mtime']	=	time();
		$no = $this->where($map)->save($ary);
		
		//删除代理返利
		model('Agent')->deleteRebate($oids,$remark);
		
		//删除支付
		$map_pay['pay_type']=	'pay_order';
		$map_pay['item_id']	=	array('in', $oids);
		$map_pay['enable']	= 	'Y';
		
		$ary_pay['enable']	=	'N';
		$ary_pay['remark']	=	$remark;
		$ary_pay['mtime']	=	time();
		M('Pay')->where($map_pay)->save($ary_pay);
		
		//删除支付
		$map_cou['order_id']	=	array('in', $oids);
		$map_cou['is_canceled']	= 	'N';
		
		$ary_cou['is_canceled']	=	'Y';
		$ary_cou['cancel_reason']	=	$remark;
		$ary_cou['cancel_time']	=	time();
		M('Coupon')->where($map_cou)->save($ary_cou);
		
		$this->commit(); 
		return $no;
	}
	
	
    /**
	 * 彻底删除订单。删除订单的同时，将删除对应的支付记录、礼券信息。
	 * @param mix $oids
	 */
	public function cleanOrder($oids){
		$this->startTrans();
		//删除支付
		$map_cou['order_id']	=	array('in', $oids);
		M('Coupon')->where($map_cou)->delete();
		
		//删除代理返利
		model('Agent')->deleteRebate($oids);
		
		//删除订单
		$map['id']	= array('in', $oids);
		$no = $this->where($map)->delete();
		
		//删除支付
		$map_pay['pay_type']=	'pay_order';
		$map_pay['item_id']	=	array('in', $oids);
		M('Pay')->where($map_pay)->delete();
		
		$this->commit(); 
		return $no;
	}
}
?>